//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class LanguageDBManager
    {
        #region Methods
        public static void DeleteLanguage(int LanguageID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LanguageDelete");
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static LanguageCollection GetAllLanguages(bool showHidden)
        {
            LanguageCollection languageCollection = new LanguageCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LanguageLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Language language = new Language();
                    language.LanguageID = NopDataHelper.GetInt(dataReader, "LanguageID");
                    language.Name = NopDataHelper.GetString(dataReader, "Name");
                    language.LanguageCulture = NopDataHelper.GetString(dataReader, "LanguageCulture");
                    language.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    language.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    languageCollection.Add(language);
                }
            }
            return languageCollection;
        }

        public static Language GetByLanguageID(int LanguageID)
        {
            Language language = null;
            if (LanguageID == 0)
                return language;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LanguageLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    language = new Language();
                    language.LanguageID = NopDataHelper.GetInt(dataReader, "LanguageID");
                    language.Name = NopDataHelper.GetString(dataReader, "Name");
                    language.LanguageCulture = NopDataHelper.GetString(dataReader, "LanguageCulture");
                    language.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    language.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                }
            }

            return language;
        }

        public static Language InsertLanguage(string Name, string LanguageCulture, bool Published, int DisplayOrder)
        {
            Language language = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LanguageInsert");
            db.AddOutParameter(dbCommand, "LanguageID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "LanguageCulture", DbType.String, LanguageCulture);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int LanguageID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@LanguageID"));
                language = GetByLanguageID(LanguageID);
            }

            return language;
        }

        public static Language UpdateLanguage(int LanguageID, string Name, string LanguageCulture, bool Published, int DisplayOrder)
        {
            Language language = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LanguageUpdate");
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "LanguageCulture", DbType.String, LanguageCulture);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                language = GetByLanguageID(LanguageID);

            return language;
        }
        #endregion
    }
}
